<!--  -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="16" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="getData()">查询</a-button>
                            <a-button icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table size="small" rowKey="id" :loading="isload"  :columns="columns" :data-source="dataList" :pagination="false"  >
          <div slot="status" slot-scope="status">
                <a-switch checked-children="上架" un-checked-children="下架" disabled :checked="status == 1 ? true : false" />
            </div>
              
             <div  slot="type" slot-scope="type" >
                 <a-tag color="#87d068" v-for="item in typeList" v-if="item.id==type" :key="item.id">
                    {{item.name}}
                 </a-tag>
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <!-- 添加、修改、删除个人推送消息 -->
        <a-modal
            title="添加、修改专区信息"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="专区名称">
                        <a-input v-model="item.name" placeholder="请输入专区名称" allow-clear></a-input>
                    </a-form-item>
                      <a-form-item label="最低价格">
                        <a-input v-model="item.minPrice" prefix="￥" type="number"  placeholder="请输入最低价格" allow-clear></a-input>
                    </a-form-item>
                      <a-form-item label="最高价格">
                        <a-input v-model="item.maxPrice" prefix="￥" type="number"  placeholder="请输入最高价格" allow-clear></a-input>
                    </a-form-item>
                     <a-form-item label="类型">
                        <a-select v-model="item.type"   >
                            <a-select-option v-for="(items) in typeList" :key="items.id">
                                {{ items.name }}
                            </a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="状态">
                        <a-radio-group v-model="item.status" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';

import { querySpecialArea , updateSpecialArea } from '@/api/mall/specialArea';
import { queryBrand }  from '@/api/brand';
export default {
    data () {
        return {
            userId: undefined,
            brandId: this.global.brandId,
            brandList: [],
            isload: false,
            dataList: [],
            size: 10,
            page: 1,
            total: 0,
            columns,
            visible: false,
            confirmLoading: false,
            item: {
                id: undefined,
                name: '',
                minPrice: '',
                maxPrice: '',
                type: '0',
                status: '1',
            },
            isAdd: true,
             statusList: [
                { id: '1', name: '是'},
                { id: '0', name: '否'},
            ],
            typeList:[
                { id: '0', name: '商城'},
                { id: '1', name: '拼团'},
                { id: '2', name: '新人专享'},
            ]
        };
    },
    async created(){
        this.getData()
    },
    methods: {
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
            }
            querySpecialArea(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 添加
        add(){
            this.isAdd = true
                this.item={
                    id: "",
                    name: '',
                    minPrice: '',
                    maxPrice: '',
                    type: '0',
                    status: '1',
                }
            this.visible = true
        },
        // 修改
        update(item){
            this.isAdd = false
            this.item = JSON.parse(JSON.stringify(item))
            this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this

            if(_this.item.name == ''){
                _this.$message.warning('请输入专区名称！');
                return
            }
            if(_this.item.minPrice == '' || _this.item.maxPrice == '' ){
                _this.$message.warning('请输入最低和最高价格');
                return
            }
            const params = {
                'brandId':_this.brandId,
                'name':_this.item.name,
                'id':_this.item.id,
                'minPrice':_this.item.minPrice,
                'maxPrice':_this.item.maxPrice,
                'type':_this.item.type,
                'status':_this.item.status,
            }
            updateSpecialArea(params)
            .then(res=>{
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                    _this.getData()
                    _this.visible = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除消息
        del(item){
            let _this = this
            const params = {
                'id':item.id,
                'isDelete':1,
            }
            updateSpecialArea(params)
            .then(res=>{
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>